package com.zy.cat.controller.game.v1;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.common.utils.DateAPIUtils;
import com.zy.cat.common.utils.TJKUtils;
import com.zy.cat.entity.game.CVSDrawHistory;
import com.zy.cat.entity.game.CVSEBottleCountHistory;
import com.zy.cat.entity.game.repository.CVSDrawHistoryRepository;
import com.zy.cat.entity.game.resp.CVSDrawHistoryResp;
import com.zy.cat.entity.game.resp.CVSInfoResp;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatGameService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "9catGameCVS", tags = { "小游戏-神奇便利店-开放API" }, description = "小游戏-神奇便利店-开放接口")
@RestController
@RequestMapping("/v1/game/cvs")
public class CatGameCVSController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatGameService catGameService;

	@Autowired
	CVSDrawHistoryRepository cvsDrawHistoryRepository;

	@PostMapping("/info")
	@ApiOperation(value = "神奇便利店", notes = "每次进入请求，返回今日礼包、游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 1, response = CVSInfoResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_uuid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_utoken_uuid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCVSInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CVSInfoResp>> result = new Callable<RSPE<CVSInfoResp>>() {
			@Override
			public RSPE<CVSInfoResp> call() throws Exception {
				return new RSPE<CVSInfoResp>(catGameService.getCVSInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/draw")
	@ApiOperation(value = "神奇便利店进行抽奖", notes = "每次进入请求，返回今日礼包、游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 2, response = CVSDrawHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_pid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getCVSDraw(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<List<CVSDrawHistoryResp>>> result = new Callable<RSPE<List<CVSDrawHistoryResp>>>() {
			@Override
			public RSPE<List<CVSDrawHistoryResp>> call() throws Exception {
				return new RSPE<List<CVSDrawHistoryResp>>(catGameService.getCVSDraw(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/draw/double")
	@ApiOperation(value = "神奇便利店进行抽奖翻倍", notes = "观看广告，进行奖励翻倍,仅支持抽奖奖励的现金豆和碎片，其他不支持翻倍！", consumes = "application/x-www-form-urlencoded", position = 3, response = CVSDrawHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fid", value = "中奖翻倍ID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_fid_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_fid_pid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getCVSDrawDouble(Integer rtype, String pid, String fid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		CVSDrawHistory draw = cvsDrawHistoryRepository.findByPidAndFid(pid, fid);

		if (draw == null || draw.getDstatus() != 1) {
			return new RSPE<Object>(-3, "抽奖信息不存在！");
		}
		if (draw.getDtype() != 0 || (draw.getZtype() != 0 && draw.getZtype() != 1)) {
			return new RSPE<Object>(-4, "此奖励不支持翻倍！");
		}
		if (draw.getFtype() == 1) {
			return new RSPE<Object>(-5, "已经翻倍，请不要重复请求！");
		}

		Callable<RSPE<CVSDrawHistoryResp>> result = new Callable<RSPE<CVSDrawHistoryResp>>() {
			@Override
			public RSPE<CVSDrawHistoryResp> call() throws Exception {
				return new RSPE<CVSDrawHistoryResp>(catGameService.getCVSDrawDouble(draw));
			}
		};
		return result;
	}

	@PostMapping("/draw/list")
	@ApiOperation(value = "神奇便利店抽奖记录", notes = "抽奖记录", consumes = "application/x-www-form-urlencoded", position = 4, response = CVSDrawHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_pid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCVSDrawList(Integer rtype, String pid, String utoken, String uuid, String token, Pages pages) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<ResultPage<CVSDrawHistoryResp>>> result = new Callable<RSPE<ResultPage<CVSDrawHistoryResp>>>() {
			@Override
			public RSPE<ResultPage<CVSDrawHistoryResp>> call() throws Exception {
				return new RSPE<ResultPage<CVSDrawHistoryResp>>(catGameService.getCVSDrawList(rtype, pid, pages));
			}
		};
		return result;
	}

	@PostMapping("/get/ebottle")
	@ApiOperation(value = "神奇便利店获取能量瓶", notes = "广告展示完毕后调用，可兑换3次，超出后，四小时后才可重新兑换", consumes = "application/x-www-form-urlencoded", position = 5, response = Map.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(utoken_uuid_uuid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "utoken_uuid_uuid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCVSEbottle(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<Map<String, Object>>> result = new Callable<RSPE<Map<String, Object>>>() {
			@Override
			public RSPE<Map<String, Object>> call() throws Exception {
				Map<String, Object> resultStr = catGameService.getCVSEbottle(rtype, pid);
				if (resultStr == null) {
					return new RSPE<Map<String, Object>>(0, "获取失败，请重试或联系客服人员！");
				}
				return new RSPE<Map<String, Object>>(resultStr);
			}
		};
		return result;
	}

	@PostMapping("/ebottle2draws")
	@ApiOperation(value = "神奇便利店能量瓶兑换抽奖次数", notes = "能量瓶兑换抽奖次数", consumes = "application/x-www-form-urlencoded", position = 6, response = RSPE.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(pid_utoken_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "pid_utoken_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getEbottle2Draws(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		CVSEBottleCountHistory countsInfo = catGameService.getTodayEbottlesCount(pid);
		if (countsInfo != null) {
			if (countsInfo.getEcount() < 1) {
				return new RSPE<Object>(-3, "能量瓶余额不足！", TJKUtils.parse2LocalDateTime(countsInfo.getLtime())
						.format(DateAPIUtils.FORMATTER_YYYYMMDDHHMMSS));
			}
		}

		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if (catGameService.getEbottle2Draws(rtype, pid)) {
					return new RSPE<Boolean>(1, "兑换成功！");
				}
				return new RSPE<Boolean>(01, "兑换失败！请重试或联系客服！");
			}
		};
		return result;
	}
	
	@PostMapping("/get/epks")
	@ApiOperation(value = "神奇便利店领取额外奖励", notes = "每日不领取，凌晨将作废", consumes = "application/x-www-form-urlencoded", position = 7, response = CVSDrawHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791",required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "eid", value = "einfo中返回的ID", example = "5371329791",required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_pid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getCVSEPkgs(Integer rtype, String pid,String eid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		
		CVSDrawHistory history=cvsDrawHistoryRepository.findByPidAndFid(pid, eid);
		if(history==null) {
			return new RSPE<Object>(-3, "额外奖励不存在！");
		}
		
		if(history.getEstatus()==1) {
			return new RSPE<Object>(-4, "额外奖励已经领取！");
		}
		
		Callable<RSPE<CVSDrawHistoryResp>> result = new Callable<RSPE<CVSDrawHistoryResp>>() {
			@Override
			public RSPE<CVSDrawHistoryResp> call() throws Exception {
				CVSDrawHistoryResp resul=catGameService.getCVSEPkgs(rtype,history);
				if(resul==null) {
					return new RSPE<CVSDrawHistoryResp>(0, "请求超时，请重试或联系客服人员！");
				}
				return new RSPE<CVSDrawHistoryResp>(resul);
			}
		};
		return result;
	}

	@PostMapping("/get/start/reward")
	@ApiOperation(value = "领取本轮首次奖励领取", notes = "神奇便利店看视频领取首轮奖励", consumes = "application/x-www-form-urlencoded", position = 8, response = String.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791",required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_pid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getCVSStartReward(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误");
		}
		//查询每日礼包记录
		List<CVSDrawHistory> list = cvsDrawHistoryRepository.findByPidAndStimeAndDtype(pid, LocalDate.now().toString(),2);
		if(list==null || list.size()<1) {
			return new RSPE<Object>(-3, "首次奖励不存在");
		}
		//判断是否为领取状态
		if (list.get(0).getDstatus()==1) {
			return new RSPE<Object>(-4, "奖励已领取");
		}
		
		Callable<RSPE<String>> result = new Callable<RSPE<String>>() {
			@Override
			public RSPE<String> call() throws Exception {
				Boolean result=catGameService.getCVSStartReward(rtype,list);
				if(result!=null && result == true) {
					return new RSPE<String>(1, "领取成功！");
				}
				return new RSPE<String>(0, "领取失败！");
			}
		};
		return result;
	}
}
